/**
 * @description 获取屏幕配置
 */
export function windowConfig() {
	let obj={
		win:uni.getWindowInfo(),
		//#ifdef MP
		mpButton:uni.getMenuButtonBoundingClientRect()
		//#endif
	}
    return obj
}


/**
 * @description url跳转
 */
export function GoUrl(url:string,act?:number) {
	if(typeof(act)!='undefined'){
		if(act===0){
			uni.reLaunch({
				url:url,
				fail: function(e){
					console.log("链接发生错误",e);
				}
			})				
		}		
		if(act===1){
			uni.redirectTo({
				url:url,
				fail: function(e){
					console.log("链接发生错误",e);
				}
			})				
		}
		if(act===2){
			uni.switchTab({
				url:url,
				fail: function(e){
					console.log("链接发生错误",e);
				}
			})				
		}
	}else{
		uni.navigateTo({
			url:url,
			fail: function(e){
				console.log("链接发生错误",e);
			}
		})		
	}
}



/**
 * @description 限制字符输出
 */
export function limtStrLen(val:string,num:number){
	if(val.length>num) return val.substring(0,num)+"..";
	return val;
}




/**
 * @description 屏幕短消息提示
 * @param  { string } messagestr 传入需要提示的字符串
 */
export function Toast(messagestr:string) {
	uni.showToast({
		icon:'none',
		title: messagestr,
		duration:1500 
	});
	setTimeout(()=>{uni.hideLoading()},1500);
}



/*隐藏部分电话号码*/
export function hiddenPhone(phonenum:string) {
	let str=phonenum.toString();
	return str.substring(0, 3) + '****' + str.substring(str.length - 4)
}



//去除左右两边空格
export function trim(s:string){
    return s.replace(/(^\s*)|(\s*$)/g,"");
}
	
	
	
//js对象数组模糊过滤模糊搜索
//let list=this.fuzzyQuery(this.list,'平次",'name');
export function fuzzyQuery(list:[],keyword:string,attribute:string) {
	const reg:RegExp = new RegExp(keyword) //创建正则表达式
	const arr:any = []
	for (let i = 0; i<list.length; i++) {
		if(reg.test(list[i][attribute])) {
			arr.push(list[i])
		}
	}
	return arr;
}


/**
 * @description 时间戳转日期 毫秒数"1531116791000"得到时间2018-07-09 14:13:11"
 * @param  { timestamp } key 时间戳
 */
export function timestampToDate(timestamp:number) {
	var d = new Date(timestamp); //得到普通的时间
	var year = d.getFullYear();
	var month = change(d.getMonth() + 1);
	var day = change(d.getDate());
	var hour = change(d.getHours());
	var minute = change(d.getMinutes());  
	var second = change(d.getSeconds());
	function change(t:number) {
		if (t < 10) {
			return "0" + t;
		} else {
			return t;
		}
	}
	var time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	return time;
}

/**
 * @description 日期转换中文时间
 * @param  { dateStr } string 2022-08-07
 * @returns  2022年08月07日
 */
export function ChineseDate(dateStr:string) {
	if(dateStr.indexOf("-")!=-1){
		let year=dateStr.split("-")[0];
		let month=dateStr.split("-")[1];
		let day=dateStr.split("-")[2];
		return year+"年"+month+"月"+day+"日";
	}
	return dateStr;
}











	
/**
 * @description 缓存
 * @param  { string } key 键值
 * @param  { string } val 值
 * @param  { expireTime } expireTime 过期时间毫秒 1000 * 60 * 0.5
 */
export interface storeType{
	key:string,
	val:string,
	expireTime:number
}
export function storeSet(key:string,val:string,expireTime:number) {
	let obj:storeType = {
		key:key,
		val:val,
		expireTime: new Date().getTime() + expireTime   // 毫秒*秒*分钟
	};
	uni.setStorageSync(key,obj);
}
export function storeGet(key:string) {
	let obj:storeType
	if(uni.getStorageSync(key)!=null){
		obj=uni.getStorageSync(key);
		const time:number = new Date().getTime(); //获取当前时间
		if (time < obj.expireTime) {
			return obj.val;
		} else {
			uni.removeStorageSync(key);
		}
	}else{
		return null;
	}
}

